package com.example.administrator.myproject.user

import android.content.Context
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast


import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.UpdateListener
import com.example.administrator.myproject.R
import com.example.administrator.myproject.bean.Collect

class OrderTakingAdapter(private val mContext: Context) : RecyclerView.Adapter<OrderTakingAdapter.Holder>() {
    private val inflater: LayoutInflater
    private var lists: List<Collect>? = null

    init {
        this.inflater = LayoutInflater.from(mContext)
    }

    override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): Holder {
        val view = inflater.inflate(R.layout.order_taking_item, viewGroup, false)
        return Holder(view)
    }

    override fun onBindViewHolder(holder: Holder, i: Int) {
        holder.tv1.text = lists!![i].name
        holder.tv2.text = lists!![i].money
        holder.tv4.text = "数量：" + lists!![i].coum!!
        if (lists!![i].state == "1") {
            holder.tv5.text = "接单"
        } else if (lists!![i].state == "2") {
            holder.tv5.text = "已接单"
        } else if (lists!![i].state == "3") {
            holder.tv5.text = "已经送达"
        }
        holder.tv5.setOnClickListener {
            if (lists!![i].state == "3") {
                println(1111)
                val collect = Collect()
                collect.coum = "1"
                collect.money = lists!![i].money
                collect.name = lists!![i].name
                collect.state = "4"
                collect.update(lists!![i].objectId, object : UpdateListener() {
                    override fun done(e: BmobException?) {
                        if (e == null) {
                            holder.tv5.text = "已送达"
                            Toast.makeText(mContext, "完成", Toast.LENGTH_SHORT).show()
                        } else {
                            Toast.makeText(mContext, "失败", Toast.LENGTH_SHORT).show()
                        }
                    }
                })
            } else if (lists!![i].state == "2") {
                Toast.makeText(mContext, "已接单", Toast.LENGTH_SHORT).show()
            } else if (lists!![i].state == "3") {
                Toast.makeText(mContext, "已完成", Toast.LENGTH_SHORT).show()
            }
        }
        if (lists!![i].name!!.equals("拌饭")) {
            holder.imageView.setImageDrawable(mContext.resources.getDrawable(R.drawable.laodiexie))
        } else if (lists!![i].name!!.equals("米饭")) {
            holder.imageView.setImageDrawable(mContext.resources.getDrawable(R.drawable.refeng))
        } else if (lists!![i].name!!.equals("米线")) {
            holder.imageView.setImageDrawable(mContext.resources.getDrawable(R.drawable.nanxie))
        } else if (lists!![i].name!!.equals("面")) {
            holder.imageView.setImageDrawable(mContext.resources.getDrawable(R.drawable.kuzi_9_fen))
        } else {
            holder.imageView.setImageDrawable(mContext.resources.getDrawable(R.drawable.mian_fan))
        }
    }

    override fun getItemCount(): Int {
        return if (lists == null) 0 else lists!!.size
    }

    fun setData(list: List<Collect>) {
        this.lists = list
        notifyDataSetChanged()
    }

    inner class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val imageView: ImageView
        val tv1: TextView
        val tv2: TextView
        val tv4: TextView
        val tv5: TextView

        init {
            imageView = itemView.findViewById(R.id.item_iv)
            tv1 = itemView.findViewById(R.id.item_tv1)
            tv2 = itemView.findViewById(R.id.item_tv2)
            tv4 = itemView.findViewById(R.id.item_tv4)
            tv5 = itemView.findViewById(R.id.item_tv5)
        }
    }
}
